package com.myth;

import org.junit.Test;

import java.io.*;

public class 字节流_字符流的使用 {

    @Test
    public void 字节流(){
        FileInputStream inputStream = null;
        FileOutputStream outputStream = null;
        File inputFile = new File("hello.txt");
        File outputFile = new File("hello\\Docker.txt");
        try{
            inputStream = new FileInputStream(inputFile);
            outputStream = new FileOutputStream(outputFile);
            byte[] buffer = new byte[5];
            int len,sum = 0;
            byte[] aaa = new byte[0];
            while((len = inputStream.read(buffer)) != -1){

                byte[] bbb = java.util.Arrays.copyOf(aaa, aaa.length+len);;
                aaa = bbb;
                System.arraycopy(buffer,0,aaa,aaa.length-len,len);
                outputStream.write(buffer,0,len);
                sum += len;
            }
            System.out.println(sum);
            System.out.println(new String(aaa));
            System.out.println(aaa);

        } catch (IOException e){
            System.out.println(e.getMessage());
        } finally {
            try {
                if (inputStream != null)
                    inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                if (outputStream != null)
                    outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void 字符流(){
        FileReader fileReader = null;
        FileWriter fileWriter = null;
        File inputFile = new File("hello.txt");
        File outputFile = new File("hello\\Docker.txt");
        try{
            fileReader = new FileReader(inputFile);
            fileWriter = new FileWriter(outputFile);
            char[] buffer = new char[10];
            int len,sum = 0;
            while((len = fileReader.read(buffer)) != -1){
                System.out.println(buffer);
                fileWriter.write(buffer,0,len);
                sum += len;
            }
            System.out.println(sum);
        } catch (IOException e){
            System.out.println(e.getMessage());
        } finally {
            try {
                if (fileReader != null)
                    fileReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                if (fileWriter != null)
                    fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
